package cn.tedu.review;

import java.util.Arrays;

public class TestArray {
    public static void main(String[] args) {
        //1.创建一个无序数组
        int[] from={1,2,3,4,5};
        System.out.println("from:"+Arrays.toString(from));
        //2.数组的简单复制
        //第一个参数：复制那个数组 第二个参数，新的长度
        int[] to= Arrays.copyOf(from,5);
        System.out.println("to:"+Arrays.toString(to));
        //3.数组的扩容
        int[] to1= Arrays.copyOf(from,10);
        System.out.println("to1:"+Arrays.toString(to1));
        //4.数组的缩容
        int[] to2= Arrays.copyOf(from,3);
        System.out.println("to2:"+Arrays.toString(to2));
        //5.指定首尾元素截取数组中的数
        //这个方法指定的是数组的下标，左闭右开，含头不含尾[2,4)，包含下标为2的元素，不包含下标为4的元素
        int[] to3= Arrays.copyOfRange(from,2,4);
        System.out.println("to3:"+Arrays.toString(to3));

        int[] from1={0,1,2,3,4,5};
        int[] to_1=Arrays.copyOf(from1,6);
        System.out.println("to_1:"+Arrays.toString(to_1));
        int[] to_2=Arrays.copyOf(from1,9);
        System.out.println("to_2:"+Arrays.toString(to_2));
        int[] to_3=Arrays.copyOf(from1,3);
        System.out.println("to_3:"+Arrays.toString(to_3));
        //参数是数组下标，左闭右开
        int[] to_4=Arrays.copyOfRange(from1,3,6);
        System.out.println("to_4:"+Arrays.toString(to_4));

        int[] from2={9,10,1,2,5,3,4,0};
        System.out.println(Arrays.toString(from2));
        System.out.println("轮数"+bubblesort(from2));;
        System.out.println(Arrays.toString(from2));

        int[] from3={0,1,2,3,4,5,6,7,8,9,8};
        System.out.println(Arrays.toString(from3));
        System.out.println("轮数"+bubblesort(from3));;

        int[] from4={7,8,9,10,0,1,2,3,4,5};
        System.out.println(Arrays.toString(from4));
        System.out.println("轮数"+bubblesort(from4));;
        System.out.println(Arrays.toString(from4));


    }

    private static int bubblesort(int[] from1) {
        int flag=0;
        //num是进行内层循环的轮数
        int num=0;
        for (int i = from1.length-1; i >=0 ; i--) {
            for (int j = 0; j <=i-1 ; j++) {
                num++;
                if (from1[j]>from1[j+1]){
                    int temp;
                    temp=from1[j+1];
                    from1[j+1]=from1[j];
                    from1[j]=temp;
                    flag=1;
                }else{
                    flag=0;
                }
            }
            if (flag==0){
                break;
            }
        }
        return num;
    }
}
